package com.superzqb.asus.superzqbFragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.baoyz.widget.PullRefreshLayout;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.squareup.picasso.Picasso;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbSave.Adapter.SaveAdapter;
import com.superzqb.asus.superzqbFragment.superzqbSave.Adapter.SaveViewAdapter;
import com.superzqb.asus.superzqbFragment.superzqbSave.AddAim;
import com.superzqb.asus.superzqbFragment.superzqbSave.MyAim;
import com.superzqb.asus.superzqbFragment.superzqbSave.StagingAssistant;
import com.superzqb.asus.superzqbFragment.superzqbSave.Welfare;
import com.superzqb.asus.superzqbUtil.MyItemClicklistener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.superzqb.asus.Api.ApiParam.imgUrl;

/**
 * Created by asus on 2017/1/17.
 */

public class SaveFragment extends Fragment implements View.OnClickListener,MyItemClicklistener {
    private Context context;
    private LinearLayout zyb, fqzs, myAim, welfare;
    private PullRefreshLayout layout;
    private ConvenientBanner convenient;//顶部广告栏控件
    private List<String> networkImages;
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private String[] images = {imgUrl+"aim2.jpg"};
    public static ArrayList<Map<String, Object>> list = new ArrayList<>();
    private SaveViewAdapter adapter;

    public SaveFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.save, container, false);
        initView(view);
        getLv();
        networkImages = Arrays.asList(images);
        convenient.setPages(new CBViewHolderCreator<SaveFragment.NetworkImageHolderView>() {
            @Override
            public SaveFragment.NetworkImageHolderView createHolder() {
                return new SaveFragment.NetworkImageHolderView();
            }
        }, networkImages);
        convenient.setCanLoop(false);


        adapter=new SaveViewAdapter(context,list);
        RecyclerView myHolder=(RecyclerView)view.findViewById(R.id.listView);
        LinearLayoutManager l=new LinearLayoutManager(getActivity());
        l.setOrientation(LinearLayoutManager.VERTICAL);
        myHolder.setLayoutManager(l);
        myHolder.setAdapter(adapter);
        myHolder.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnItemClickListener(this);
      /*  listView.setAdapter(adapter);*/
     /*   layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        getLv();
                    }
                }, 2000);
            }
        });*/


        return view;
    }
    public void getLv() {
        list.clear();
        for(int i=0;i<1;i++) {
            Map<String, Object> map = new HashMap<>();
            //键一般为"字符串"
            map.put("aimPrice", "4180");
            map.put("finishPrice", "3800");
            map.put("aimFinish", "80");
            map.put("finishTime", "15");
            map.put("must", "10");
            list.add(map);
        }

    }

    private void initView(View view) {
        context=getActivity();
        zyb = (LinearLayout) view.findViewById(R.id.zyb);
        fqzs = (LinearLayout) view.findViewById(R.id.fqzs);
        myAim = (LinearLayout) view.findViewById(R.id.myAim);
        welfare = (LinearLayout) view.findViewById(R.id.welfare);
        convenient = (ConvenientBanner) view.findViewById(R.id.convenientBanner);
        layout= (PullRefreshLayout)view.findViewById(R.id.swipeRefreshLayout);
        li = (LinearLayout) view.findViewById(R.id.play);
        /*获取屏幕的宽高*/
        Display d = getActivity().getWindowManager().getDefaultDisplay();
        /*获取布局参数*/
        i = li.getLayoutParams();
        i.height = d.getHeight() / 4;
        i.width = d.getWidth();
        zyb.setOnClickListener(this);
        fqzs.setOnClickListener(this);
        myAim.setOnClickListener(this);
        welfare.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fqzs:
                // Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getActivity(), StagingAssistant.class);
                getActivity().startActivity(intent);
                break;
            case R.id.welfare:
                // Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();

                Intent intent1 = new Intent(getActivity(), Welfare.class);
                getActivity().startActivity(intent1);
                break;
            case R.id.myAim:
                // Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();

                Intent intent2 = new Intent(getActivity(), MyAim.class);
                getActivity().startActivity(intent2);
                break;
            case R.id.zyb:
                Intent intent3 = new Intent(getActivity(), AddAim.class);

                getActivity().startActivity(intent3);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(View view, int postion) {

    }

    private class NetworkImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
          /*  Picasso.with(getActivity()).load(data).transform(new BitmapTransformation(i)).into(imageView);*/
           Picasso.with(getActivity()).load(data).resize(i.width, i.height).into(imageView);
           // imageView.setImageResource(R.mipmap.save_bg);
        }
    }
    @Override
    public void onResume() {
        super.onResume();

    }
}
